// APP
if (window.plus) {
    loginPageReady();
} else {
    document.addEventListener('plusready', loginPageReady, false);
}

// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        loginPageReady();
    }
})

function loginPageReady() {
    var _currentloginuser = getLocalStorage(STORAGE_MANAGER.user);
    if (_currentloginuser) {
        back();
        return;
    }
    var loginPage = function () {
        this.init();
    };
    loginPage.prototype = {
        // 初始化
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            //记住登陆用户名
            $("#username").val(getLocalStorage(STORAGE_MANAGER.username));
            $.getIPAddress();
        },
        //绑定事件
        bindEvent: function () {
//          $("#username").focus();

            //点击回退
            $("#back").on("click", function () {
                back();
            });

            //点击注册
            $("#register").on("click", function () {
                redirect("../account/register.html");
            });

            //点击找回密码
            $("#forgetPass").on("click", function () {
                redirect('../account/refindpass.html');
            });

            // 第三方登录
            $("#btnThirdLogin>span").on("click", function () {
                var typeid = $(this).attr("tip");
                if (AGENT_CLIENT.APP) {
                    _thirdaccountPage.thirdLogin(typeid);
                } else {
                    switch (typeid) {
                        case "qq":
                            redirect("../account/login_qq.html");
                            break;
                        case "weixin":
                            redirect("../account/login_wechat.html");
                            break;
                        case "sinaweibo":
                            redirect("../account/login_sina.html");
                            break;
                        default:
                            break;
                    }
                    return;
                }
            });

            $("#username,#password").on("input", function () {
                if ("" == $("#username").val() || "" == $("#password").val()) {
                    $("#login").removeClass("liang");
                } else {
                    $("#login").addClass("liang");
                }
            });

            //点击登陆
            $("#login").on("click", function () {
                if (!$("#login").hasClass("liang")) {
                    return;
                }
                _verfiyAccountModel.userlogin($("#username").val(), $("#password").val());
            });
        }
    };

    var _loginPage = new loginPage();

    // 验证用户信息
    var _verfiyAccountModel = new verfiyAccountModel();

    // 第三方登录
    var _thirdaccountPage = new thirdaccountPage();
    _thirdaccountPage.getUserInfo = function (userinfo) {
        _verfiyAccountModel.verifyAccount(userinfo);
    };
}

